package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author fanYuLe
 * @Date 2023/3/8 18:02
 * @Blog blog.fanYuLe.net/996.mba
 * @Description:
 */
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    //h5菜品查询
    @GetMapping("/dish/list")
    public ResultInfo list(Long categoryId,Integer status){
        List<Dish> list = null;
        //1.先查询redis缓存
        //2.设置redisKey
        String dishKey="dish_"+categoryId;
        //3判断key是否存在
        if (redisTemplate.hasKey(dishKey)) {
            //3.1存在，查询redis
            list = (List<Dish>) redisTemplate.opsForValue().get(dishKey);
        }else {
            //3.2不存在，查询数据库
            list=dishService.list(categoryId,status);
            //3.2.1同步到redis
            redisTemplate.opsForValue().set(dishKey,list);
        }
        //4返回结果
        return  ResultInfo.success(list);
    }
}

